
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Introduction to class-based views &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Built-in class-based generic views" href="generic-display.html" />
    <link rel="prev" title="Class-based views" href="index.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="index.html" title="Class-based views">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="generic-display.html" title="Built-in class-based generic views">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-class-based-views-intro">
            
  <div class="section" id="s-introduction-to-class-based-views">
<span id="introduction-to-class-based-views"></span><h1>Introduction to class-based views<a class="headerlink" href="#introduction-to-class-based-views" title="Permalink to this headline">¶</a></h1>
<p>Class-based views provide an alternative way to implement views as Python
objects instead of functions. They do not replace function-based views, but
have certain differences and advantages when compared to function-based views:</p>
<ul class="simple">
<li>Organization of code related to specific HTTP methods (<code class="docutils literal notranslate"><span class="pre">GET</span></code>, <code class="docutils literal notranslate"><span class="pre">POST</span></code>,
etc.) can be addressed by separate methods instead of conditional branching.</li>
<li>Object oriented techniques such as mixins (multiple inheritance) can be
used to factor code into reusable components.</li>
</ul>
<div class="section" id="s-the-relationship-and-history-of-generic-views-class-based-views-and-class-based-generic-views">
<span id="the-relationship-and-history-of-generic-views-class-based-views-and-class-based-generic-views"></span><h2>The relationship and history of generic views, class-based views, and class-based generic views<a class="headerlink" href="#the-relationship-and-history-of-generic-views-class-based-views-and-class-based-generic-views" title="Permalink to this headline">¶</a></h2>
<p>In the beginning there was only the view function contract, Django passed your
function an <a class="reference internal" href="../../ref/request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpRequest</span></code></a> and expected back an
<a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a>. This was the extent of what Django provided.</p>
<p>Early on it was recognized that there were common idioms and patterns found in
view development. Function-based generic views were introduced to abstract
these patterns and ease view development for the common cases.</p>
<p>The problem with function-based generic views is that while they covered the
simple cases well, there was no way to extend or customize them beyond some
simple configuration options, limiting their usefulness in many real-world
applications.</p>
<p>Class-based generic views were created with the same objective as
function-based generic views, to make view development easier. However, the way
the solution is implemented, through the use of mixins, provides a toolkit that
results in class-based generic views being more extensible and flexible than
their function-based counterparts.</p>
<p>If you have tried function based generic views in the past and found them
lacking, you should not think of class-based generic views as simply a
class-based equivalent, but rather as a fresh approach to solving the original
problems that generic views were meant to solve.</p>
<p>The toolkit of base classes and mixins that Django uses to build class-based
generic views are built for maximum flexibility, and as such have many hooks in
the form of default method implementations and attributes that you are unlikely
to be concerned with in the simplest use cases. For example, instead of
limiting you to a class-based attribute for <code class="docutils literal notranslate"><span class="pre">form_class</span></code>, the implementation
uses a <code class="docutils literal notranslate"><span class="pre">get_form</span></code> method, which calls a <code class="docutils literal notranslate"><span class="pre">get_form_class</span></code> method, which in
its default implementation just returns the <code class="docutils literal notranslate"><span class="pre">form_class</span></code> attribute of the
class. This gives you several options for specifying what form to use, from a
simple attribute, to a fully dynamic, callable hook. These options seem to add
hollow complexity for simple situations, but without them, more advanced
designs would be limited.</p>
</div>
<div class="section" id="s-using-class-based-views">
<span id="using-class-based-views"></span><h2>Using class-based views<a class="headerlink" href="#using-class-based-views" title="Permalink to this headline">¶</a></h2>
<p>At its core, a class-based view allows you to respond to different HTTP request
methods with different class instance methods, instead of with conditionally
branching code inside a single view function.</p>
<p>So where the code to handle HTTP <code class="docutils literal notranslate"><span class="pre">GET</span></code> in a view function would look
something like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponse</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;GET&#39;</span><span class="p">:</span>
        <span class="c1"># &lt;view logic&gt;</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s1">&#39;result&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>In a class-based view, this would become:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponse</span>
<span class="kn">from</span> <span class="nn">django.views</span> <span class="k">import</span> <span class="n">View</span>

<span class="k">class</span> <span class="nc">MyView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="c1"># &lt;view logic&gt;</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s1">&#39;result&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Because Django’s URL resolver expects to send the request and associated
arguments to a callable function, not a class, class-based views have an
<a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.View.as_view" title="django.views.generic.base.View.as_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">as_view()</span></code></a> class method which returns a
function that can be called when a request arrives for a URL matching the
associated pattern. The function creates an instance of the class and calls its
<a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.View.dispatch" title="django.views.generic.base.View.dispatch"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dispatch()</span></code></a> method. <code class="docutils literal notranslate"><span class="pre">dispatch</span></code> looks at
the request to determine whether it is a <code class="docutils literal notranslate"><span class="pre">GET</span></code>, <code class="docutils literal notranslate"><span class="pre">POST</span></code>, etc, and relays the
request to a matching method if one is defined, or raises
<a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponseNotAllowed" title="django.http.HttpResponseNotAllowed"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponseNotAllowed</span></code></a> if not:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># urls.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">myapp.views</span> <span class="k">import</span> <span class="n">MyView</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^about/$&#39;</span><span class="p">,</span> <span class="n">MyView</span><span class="o">.</span><span class="n">as_view</span><span class="p">()),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>It is worth noting that what your method returns is identical to what you
return from a function-based view, namely some form of
<a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a>. This means that
<a class="reference internal" href="../http/shortcuts.html"><span class="doc">http shortcuts</span></a> or
<a class="reference internal" href="../../ref/template-response.html#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemplateResponse</span></code></a> objects are valid to use
inside a class-based view.</p>
<p>While a minimal class-based view does not require any class attributes to
perform its job, class attributes are useful in many class-based designs,
and there are two ways to configure or set class attributes.</p>
<p>The first is the standard Python way of subclassing and overriding attributes
and methods in the subclass. So that if your parent class had an attribute
<code class="docutils literal notranslate"><span class="pre">greeting</span></code> like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponse</span>
<span class="kn">from</span> <span class="nn">django.views</span> <span class="k">import</span> <span class="n">View</span>

<span class="k">class</span> <span class="nc">GreetingView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">greeting</span> <span class="o">=</span> <span class="s2">&quot;Good Day&quot;</span>

    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">greeting</span><span class="p">)</span>
</pre></div>
</div>
<p>You can override that in a subclass:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MorningGreetingView</span><span class="p">(</span><span class="n">GreetingView</span><span class="p">):</span>
    <span class="n">greeting</span> <span class="o">=</span> <span class="s2">&quot;Morning to ya&quot;</span>
</pre></div>
</div>
<p>Another option is to configure class attributes as keyword arguments to the
<a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.View.as_view" title="django.views.generic.base.View.as_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">as_view()</span></code></a> call in the URLconf:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^about/$&#39;</span><span class="p">,</span> <span class="n">GreetingView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="n">greeting</span><span class="o">=</span><span class="s2">&quot;G&#39;day&quot;</span><span class="p">)),</span>
<span class="p">]</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">While your class is instantiated for each request dispatched to it, class
attributes set through the
<a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.View.as_view" title="django.views.generic.base.View.as_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">as_view()</span></code></a> entry point are
configured only once at the time your URLs are imported.</p>
</div>
</div>
<div class="section" id="s-using-mixins">
<span id="using-mixins"></span><h2>Using mixins<a class="headerlink" href="#using-mixins" title="Permalink to this headline">¶</a></h2>
<p>Mixins are a form of multiple inheritance where behaviors and attributes of
multiple parent classes can be combined.</p>
<p>For example, in the generic class-based views there is a mixin called
<a class="reference internal" href="../../ref/class-based-views/mixins-simple.html#django.views.generic.base.TemplateResponseMixin" title="django.views.generic.base.TemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemplateResponseMixin</span></code></a> whose primary purpose
is to define the method
<a class="reference internal" href="../../ref/class-based-views/mixins-simple.html#django.views.generic.base.TemplateResponseMixin.render_to_response" title="django.views.generic.base.TemplateResponseMixin.render_to_response"><code class="xref py py-meth docutils literal notranslate"><span class="pre">render_to_response()</span></code></a>.
When combined with the behavior of the <a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.View" title="django.views.generic.base.View"><code class="xref py py-class docutils literal notranslate"><span class="pre">View</span></code></a>
base class, the result is a <a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.TemplateView" title="django.views.generic.base.TemplateView"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemplateView</span></code></a>
class that will dispatch requests to the appropriate matching methods (a
behavior defined in the <code class="docutils literal notranslate"><span class="pre">View</span></code> base class), and that has a
<a class="reference internal" href="../../ref/class-based-views/mixins-simple.html#django.views.generic.base.TemplateResponseMixin.render_to_response" title="django.views.generic.base.TemplateResponseMixin.render_to_response"><code class="xref py py-meth docutils literal notranslate"><span class="pre">render_to_response()</span></code></a>
method that uses a
<a class="reference internal" href="../../ref/class-based-views/mixins-simple.html#django.views.generic.base.TemplateResponseMixin.template_name" title="django.views.generic.base.TemplateResponseMixin.template_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">template_name</span></code></a>
attribute to return a <a class="reference internal" href="../../ref/template-response.html#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemplateResponse</span></code></a>
object (a behavior defined in the <code class="docutils literal notranslate"><span class="pre">TemplateResponseMixin</span></code>).</p>
<p>Mixins are an excellent way of reusing code across multiple classes, but they
come with some cost. The more your code is scattered among mixins, the harder
it will be to read a child class and know what exactly it is doing, and the
harder it will be to know which methods from which mixins to override if you
are subclassing something that has a deep inheritance tree.</p>
<p>Note also that you can only inherit from one generic view - that is, only one
parent class may inherit from <a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.View" title="django.views.generic.base.View"><code class="xref py py-class docutils literal notranslate"><span class="pre">View</span></code></a> and
the rest (if any) should be mixins. Trying to inherit from more than one class
that inherits from <code class="docutils literal notranslate"><span class="pre">View</span></code> - for example, trying to use a form at the top of a
list and combining <a class="reference internal" href="../../ref/class-based-views/mixins-editing.html#django.views.generic.edit.ProcessFormView" title="django.views.generic.edit.ProcessFormView"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessFormView</span></code></a> and
<a class="reference internal" href="../../ref/class-based-views/generic-display.html#django.views.generic.list.ListView" title="django.views.generic.list.ListView"><code class="xref py py-class docutils literal notranslate"><span class="pre">ListView</span></code></a> - won’t work as expected.</p>
</div>
<div class="section" id="s-handling-forms-with-class-based-views">
<span id="handling-forms-with-class-based-views"></span><h2>Handling forms with class-based views<a class="headerlink" href="#handling-forms-with-class-based-views" title="Permalink to this headline">¶</a></h2>
<p>A basic function-based view that handles forms may look something like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponseRedirect</span>
<span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="k">import</span> <span class="n">render</span>

<span class="kn">from</span> <span class="nn">.forms</span> <span class="k">import</span> <span class="n">MyForm</span>

<span class="k">def</span> <span class="nf">myview</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s2">&quot;POST&quot;</span><span class="p">:</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">MyForm</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
            <span class="c1"># &lt;process form cleaned data&gt;</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="s1">&#39;/success/&#39;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">MyForm</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;key&#39;</span><span class="p">:</span> <span class="s1">&#39;value&#39;</span><span class="p">})</span>

    <span class="k">return</span> <span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">&#39;form_template.html&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">})</span>
</pre></div>
</div>
<p>A similar class-based view might look like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponseRedirect</span>
<span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="k">import</span> <span class="n">render</span>
<span class="kn">from</span> <span class="nn">django.views</span> <span class="k">import</span> <span class="n">View</span>

<span class="kn">from</span> <span class="nn">.forms</span> <span class="k">import</span> <span class="n">MyForm</span>

<span class="k">class</span> <span class="nc">MyFormView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">form_class</span> <span class="o">=</span> <span class="n">MyForm</span>
    <span class="n">initial</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;key&#39;</span><span class="p">:</span> <span class="s1">&#39;value&#39;</span><span class="p">}</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;form_template.html&#39;</span>

    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">form</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">form_class</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">initial</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">template_name</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">})</span>

    <span class="k">def</span> <span class="nf">post</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">form</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">form_class</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
            <span class="c1"># &lt;process form cleaned data&gt;</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="s1">&#39;/success/&#39;</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">template_name</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">})</span>
</pre></div>
</div>
<p>This is a very simple case, but you can see that you would then have the option
of customizing this view by overriding any of the class attributes, e.g.
<code class="docutils literal notranslate"><span class="pre">form_class</span></code>, via URLconf configuration, or subclassing and overriding one or
more of the methods (or both!).</p>
</div>
<div class="section" id="s-decorating-class-based-views">
<span id="decorating-class-based-views"></span><h2>Decorating class-based views<a class="headerlink" href="#decorating-class-based-views" title="Permalink to this headline">¶</a></h2>
<p>The extension of class-based views isn’t limited to using mixins. You can also
use decorators. Since class-based views aren’t functions, decorating them works
differently depending on if you’re using <code class="docutils literal notranslate"><span class="pre">as_view()</span></code> or creating a subclass.</p>
<div class="section" id="s-decorating-in-urlconf">
<span id="decorating-in-urlconf"></span><h3>Decorating in URLconf<a class="headerlink" href="#decorating-in-urlconf" title="Permalink to this headline">¶</a></h3>
<p>The simplest way of decorating class-based views is to decorate the
result of the <a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.View.as_view" title="django.views.generic.base.View.as_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">as_view()</span></code></a> method.
The easiest place to do this is in the URLconf where you deploy your view:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.auth.decorators</span> <span class="k">import</span> <span class="n">login_required</span><span class="p">,</span> <span class="n">permission_required</span>
<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="k">import</span> <span class="n">TemplateView</span>

<span class="kn">from</span> <span class="nn">.views</span> <span class="k">import</span> <span class="n">VoteView</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^about/$&#39;</span><span class="p">,</span> <span class="n">login_required</span><span class="p">(</span><span class="n">TemplateView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="n">template_name</span><span class="o">=</span><span class="s2">&quot;secret.html&quot;</span><span class="p">))),</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^vote/$&#39;</span><span class="p">,</span> <span class="n">permission_required</span><span class="p">(</span><span class="s1">&#39;polls.can_vote&#39;</span><span class="p">)(</span><span class="n">VoteView</span><span class="o">.</span><span class="n">as_view</span><span class="p">())),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>This approach applies the decorator on a per-instance basis. If you
want every instance of a view to be decorated, you need to take a
different approach.</p>
</div>
<div class="section" id="s-decorating-the-class">
<span id="s-id1"></span><span id="decorating-the-class"></span><span id="id1"></span><h3>Decorating the class<a class="headerlink" href="#decorating-the-class" title="Permalink to this headline">¶</a></h3>
<p>To decorate every instance of a class-based view, you need to decorate
the class definition itself. To do this you apply the decorator to the
<a class="reference internal" href="../../ref/class-based-views/base.html#django.views.generic.base.View.dispatch" title="django.views.generic.base.View.dispatch"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dispatch()</span></code></a> method of the class.</p>
<p>A method on a class isn’t quite the same as a standalone function, so
you can’t just apply a function decorator to the method – you need to
transform it into a method decorator first. The <code class="docutils literal notranslate"><span class="pre">method_decorator</span></code>
decorator transforms a function decorator into a method decorator so
that it can be used on an instance method. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.auth.decorators</span> <span class="k">import</span> <span class="n">login_required</span>
<span class="kn">from</span> <span class="nn">django.utils.decorators</span> <span class="k">import</span> <span class="n">method_decorator</span>
<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="k">import</span> <span class="n">TemplateView</span>

<span class="k">class</span> <span class="nc">ProtectedView</span><span class="p">(</span><span class="n">TemplateView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;secret.html&#39;</span>

    <span class="nd">@method_decorator</span><span class="p">(</span><span class="n">login_required</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">dispatch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">ProtectedView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">dispatch</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p>Or, more succinctly, you can decorate the class instead and pass the name
of the method to be decorated as the keyword argument <code class="docutils literal notranslate"><span class="pre">name</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nd">@method_decorator</span><span class="p">(</span><span class="n">login_required</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;dispatch&#39;</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">ProtectedView</span><span class="p">(</span><span class="n">TemplateView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;secret.html&#39;</span>
</pre></div>
</div>
<p>If you have a set of common decorators used in several places, you can define
a list or tuple of decorators and use this instead of invoking
<code class="docutils literal notranslate"><span class="pre">method_decorator()</span></code> multiple times. These two classes are equivalent:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">decorators</span> <span class="o">=</span> <span class="p">[</span><span class="n">never_cache</span><span class="p">,</span> <span class="n">login_required</span><span class="p">]</span>

<span class="nd">@method_decorator</span><span class="p">(</span><span class="n">decorators</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;dispatch&#39;</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">ProtectedView</span><span class="p">(</span><span class="n">TemplateView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;secret.html&#39;</span>

<span class="nd">@method_decorator</span><span class="p">(</span><span class="n">never_cache</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;dispatch&#39;</span><span class="p">)</span>
<span class="nd">@method_decorator</span><span class="p">(</span><span class="n">login_required</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;dispatch&#39;</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">ProtectedView</span><span class="p">(</span><span class="n">TemplateView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;secret.html&#39;</span>
</pre></div>
</div>
<p>The decorators will process a request in the order they are passed to the
decorator. In the example, <code class="docutils literal notranslate"><span class="pre">never_cache()</span></code> will process the request before
<code class="docutils literal notranslate"><span class="pre">login_required()</span></code>.</p>
<p>In this example, every instance of <code class="docutils literal notranslate"><span class="pre">ProtectedView</span></code> will have login protection.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><code class="docutils literal notranslate"><span class="pre">method_decorator</span></code> passes <code class="docutils literal notranslate"><span class="pre">*args</span></code> and <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code>
as parameters to the decorated method on the class. If your method
does not accept a compatible set of parameters it will raise a
<code class="docutils literal notranslate"><span class="pre">TypeError</span></code> exception.</p>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Introduction to class-based views</a><ul>
<li><a class="reference internal" href="#the-relationship-and-history-of-generic-views-class-based-views-and-class-based-generic-views">The relationship and history of generic views, class-based views, and class-based generic views</a></li>
<li><a class="reference internal" href="#using-class-based-views">Using class-based views</a></li>
<li><a class="reference internal" href="#using-mixins">Using mixins</a></li>
<li><a class="reference internal" href="#handling-forms-with-class-based-views">Handling forms with class-based views</a></li>
<li><a class="reference internal" href="#decorating-class-based-views">Decorating class-based views</a><ul>
<li><a class="reference internal" href="#decorating-in-urlconf">Decorating in URLconf</a></li>
<li><a class="reference internal" href="#decorating-the-class">Decorating the class</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="index.html"
                        title="previous chapter">Class-based views</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="generic-display.html"
                        title="next chapter">Built-in class-based generic views</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/topics/class-based-views/intro.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="index.html" title="Class-based views">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="generic-display.html" title="Built-in class-based generic views">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>